/* GUI CONVERTED from apvocsu1.p (converter v1.76) Tue Mar 18 16:12:37 2003 */
/* apvocsu1.p - AP VOUCHER INVENTORY COST UPDATE                              */
/* Copyright 1986-2003 QAD Inc., Carpinteria, CA, USA.                        */
/* All rights reserved worldwide.  This is an unpublished work.               */
/* $Revision: 1.21.1.15 $                                               */
/*V8:ConvertMode=Maintenance                                                  */
/* REVISION: 7.4           CREATED: 08/31/95   BY: jzw *H0FR*                 */
/*                   code moved from apvomtk3.p and modified here             */
/*                   LAST MODIFIED: 01/22/96   BY: jzw *G1KH*                 */
/*                                  01/26/96   BY: jzw *H0J8*                 */
/* REVISION: 8.5     LAST MODIFIED: 09/26/95   by: mwd *J053*                 */
/*                                  03/12/96   by: jzw *G1Q6*                 */
/*                                  03/28/96   by: jzw *G1RQ*                 */
/*                                  07/25/96   by: jwk *G29Q*                 */
/* REVISION: 8.5     LAST MODIFIED: 07/26/96   BY: *J10X* Markus Barone       */
/* REVISION: 8.6     LAST MODIFIED: 09/03/96   by: jzw *K008*                 */
/*                                  11/25/96   by: jzw *K01X*                 */
/*                                  12/04/96   by: EJH *K013*                 */
/*                                  01/13/97   by: rxm *H0R4*                 */
/*                                  02/21/97   by: rxm *H0RQ*                 */
/* REVISION: 8.6     LAST MODIFIED: 10/13/97   BY: *H0ZP* Samir Bavkar        */
/* REVISION: 8.6E    LAST MOFIFIED: 02/23/98   BY: *H1JT* Allen Licha         */
/* REVISION: 8.6E    LAST MOFIFIED: 04/27/98   BY: *H1KQ* Samir Bavkar        */
/* REVISION: 8.6E    LAST MODIFIED: 05/09/98   BY: *L00Y* Jeff Wootton        */
/* Pre-86E commented code removed, view in archive revision 1.18              */
/* Old ECO marker removed, but no ECO header exists *FT67*                    */
/* REVISION: 8.6E    LAST MODIFIED: 07/30/98   BY: *L03K* Jeff Wootton        */
/* Pre-86E commented code removed, view in archive revision 1.19              */
/* REVISION: 9.0     LAST MODIFIED: 03/10/99   BY: *M0B3* Michael Amaladhas   */
/* REVISION: 9.0     LAST MODIFIED: 03/13/99   BY: *M0BD* Alfred Tan          */
/* REVISION: 9.0     LAST MODIFIED: 03/15/99   BY: *M0BG* Jeff Wootton        */
/* REVISION: 9.1     LAST MODIFIED: 10/01/99   BY: *N014* Murali Ayyagari     */
/* REVISION: 9.1     LAST MODIFIED: 11/01/99   BY: *N053* Jeff Wootton        */
/* REVISION: 9.1     LAST MODIFIED: 08/11/00   BY: *N0KK* jyn                 */
/* Revision: 1.21.1.7     BY: Katie Hilbert  DATE: 04/01/01 ECO: *P002* */
/* Revision: 1.21.1.9     BY: Santosh Rao    DATE: 02/12/02 ECO: *N190* */
/* Revision: 1.21.1.10    BY: Rajiv Ramaiah  DATE: 03/20/02 ECO: *M1WW* */
/* Revision: 1.21.1.12    BY: Patrick Rowan  DATE: 04/17/02 ECO: *P043* */
/* Revision: 1.21.1.13    BY: Patrick Rowan  DATE: 04/17/02 ECO: *P05Q* */
/* Revision: 1.21.1.14     BY: Patrick Rowan        DATE: 05/17/02  ECO: *P018*  */
/* $Revision: 1.21.1.15 $       BY: Tiziana Giustozzi   DATE: 02/20/03 ECO: *P0MX* */
/******************************************************************************/
/* All patch markers and commented out code have been removed from the source */
/* code below. For all future modifications to this file, any code which is   */
/* no longer required should be deleted and no in-line patch markers should   */
/* be added.  The ECO marker should only be included in the Revision History. */
/******************************************************************************/

/************************************************************************/
/* This program is run from the following programs, at Confirmation:-   */
/*  Apvomtk3.p  AP Voucher Maintenance, Cost Updates                    */
/*  Apvoco.p    AP Voucher Confirmation - Manual                        */
/*  Apvoco01.p  AP Voucher Confirmation - Automatic                     */
/************************************************************************/

{mfdeclre.i}

define variable totl_amt       like glt_amt.
define variable unit_amt       like glt_amt.
define variable l_glx_mthd     like cs_method.
define variable l_glx_set      like cs_set.
define variable l_cur_mthd     like cs_method.
define variable l_cur_set      like cs_set.

define variable rate_var_amt   like vod_amt no-undo.
define variable i              as integer.
define variable taxamt         like glt_amt no-undo.
define variable curr_cost_taxamt like glt_amt no-undo.
define variable taxamt_this_tx2d like glt_amt no-undo.
define variable gl_tmp_amt     as decimal.

define variable tax_on_vph_price like sct_mtl_tl.
define variable tax_on_receipt like sct_mtl_tl.
define variable rcpt_tr_type   like tx2d_tr_type no-undo.

define variable tax_ref        like tx2d_ref     no-undo.
define variable tax_nbr        like tx2d_nbr     no-undo.
define variable mc-error-number like msg_nbr no-undo.
define variable rcp_date_vo_ex_rate  like vo_ex_rate no-undo.
define variable rcp_date_vo_ex_rate2 like vo_ex_rate2 no-undo.
define variable old_db         like si_db no-undo.
define variable l_po_site      like si_site no-undo.
define variable l_gl_ref       like glt_ref no-undo.

define variable l_vph_cf_adj_amt  like vph_cf_adj_amt   no-undo.
define variable l_vph_cf_dscr_amt like vph_cf_dscr_amt  no-undo.

define shared variable ap_recno        as recid.
define shared variable vo_recno        as recid.
define shared variable vph_recno       as recid.

define shared variable rndmthd         like rnd_rnd_mthd.

define new shared variable new_site like si_site.
define new shared variable new_db   like si_db.

define variable p-prh-pur-cost like prh_pur_cost.
{apconsdf.i}

for first ap_mstr
      fields(ap_ref
      ap_acct
      ap_sub
      ap_cc)
      where recid(ap_mstr) = ap_recno
      no-lock:
end.

for first vo_mstr
      fields(vo_ref
      vo_curr
      vo_ex_rate
      vo_ex_rate2
      vo_ex_ratetype)
      where recid(vo_mstr) = vo_recno
      no-lock:
end.

find vph_hist
   where recid(vph_hist) = vph_recno
   exclusive-lock.

for first pvo_mstr
   where pvo_id = vph_pvo_id
     and pvo_internal_ref_type = {&TYPE_POReceiver}
     and pvo_lc_charge    = ""
   no-lock:
end.

for first pod_det
      fields(pod_wo_lot pod_project pod_consignment
      pod_op)
      where pod_nbr = pvo_order
      and pod_line = pvo_line
      no-lock:
end.


for first gl_ctrl
      fields(gl_rnd_mthd)
      no-lock:
end.

for first prh_hist
      fields(prh_po_site
      prh_type
      prh_um_conv
      prh_nbr
      prh_line
      prh_receiver
      prh_pur_cost
      prh_curr_amt
      prh_um_conv)
      where prh_receiver = pvo_internal_ref
      and prh_line = pvo_line
      no-lock:
end.

assign

   /* RETAIN COST ADJUSTMENT AMOUNT IN VPH_ADJ_AMT */

   vph_adj_prv_cst = 0
   vph_qoh_at_adj = 0

   /* RETAIN COST ADJUSTMENT DISCREPANCY AMOUNT IN VPH_DSCR_AMT */

   vph_cf_adj_amt = 0
   vph_cf_dscr_amt = 0
   vph_costadj_acct = ""
   vph_costadj_sub = ""
   vph_costadj_cc = ""
   vph_dscr_acct = ""
   vph_dscr_sub = ""
   vph_dscr_cc = ""

   gl_tmp_amt  = (vph_curr_amt * vph_inv_qty).

{gprunp.i "mcpl" "p" "mc-curr-rnd"
   "(input-output gl_tmp_amt,
     input rndmthd,
     output mc-error-number)"}
if mc-error-number <> 0 then do:
   {pxmsg.i &MSGNUM=mc-error-number &ERRORLEVEL=2}
end.

rate_var_amt = gl_tmp_amt.

gl_tmp_amt = (prh_curr_amt * prh_um_conv * vph_inv_qty).

{gprunp.i "mcpl" "p" "mc-curr-rnd"
   "(input-output gl_tmp_amt,
     input rndmthd,
     output mc-error-number)"}
if mc-error-number <> 0 then do:
   {pxmsg.i &MSGNUM=mc-error-number &ERRORLEVEL=2}
end.

/* CONVERT, SIMILAR TO ETK ECO G2SZ IN APVOMTA5.P */
if vo_curr <> pvo_curr then do:
   /* GET EXCHANGE RATE BETWEEN VOUCHER AND BASE */
   /* AS OF RECEIPT DATE */
   {gprunp.i "mcpl" "p" "mc-get-ex-rate"
      "(input vo_curr,
        input base_curr,
        input vo_ex_ratetype,
        input pvo_trans_date,
        output rcp_date_vo_ex_rate,
        output rcp_date_vo_ex_rate2,
        output mc-error-number)"}
   if mc-error-number <> 0 then do:
      {pxmsg.i &MSGNUM=mc-error-number &ERRORLEVEL=2}
   end.
   gl_tmp_amt = gl_tmp_amt * rcp_date_vo_ex_rate
   / rcp_date_vo_ex_rate2 * pvo_ex_rate2
   / pvo_ex_rate.
end.



rate_var_amt = rate_var_amt - gl_tmp_amt.


if vo_curr <> base_curr
   and vo_ex_rate <> 0
   and vo_ex_rate2 <> 0 then
   do:

   /* CONVERT FROM FOREIGN TO BASE CURRENCY */
   /* CHANGED SIXTH INPUT PARAMETER FROM true TO false TO AVOID  */
   /* ROUNDING OF totl_amt                                       */

   {gprunp.i "mcpl" "p" "mc-curr-conv"
      "(input vo_curr,
        input base_curr,
        input vo_ex_rate,
        input vo_ex_rate2,
        input rate_var_amt,
        input false,
        output totl_amt,
        output mc-error-number)"}.
   if mc-error-number <> 0 then do:
      {pxmsg.i &MSGNUM=mc-error-number &ERRORLEVEL=2}
   end.

end.
else

totl_amt = rate_var_amt.
 
unit_amt = totl_amt / (vph_inv_qty * prh_um_conv).
if unit_amt = ? then unit_amt = 0.
if totl_amt < 0 and unit_amt > 0
   or totl_amt > 0 and unit_amt < 0
   then unit_amt = - unit_amt.

if prh_type = "" then do:
   if pvo_cost_update  then do:
      curr_cost_taxamt = 0.
      run calc-current-cost(vph_inv_cost / prh_um_conv).
   end.

   /*ADD TAX TO TOTL_AMT.  ALSO NOTE THAT QAD_DECFLD[1] IS SET */
   /*EQUAL TO TOTL_AMT AFTER THIS SO QAD_DECFLD[1] WILL INCLUDE*/
   /*TAX.  THIS IS NECESSARY SINCE IF REVERSE OUT RECEIPT NEED */
   /*TO INCLUDE TAX.                                           */
   assign
      tax_on_receipt = 0
      taxamt = 0
      tax_on_vph_price = 0.

   rcpt_tr_type = "22". /* VOUCHER */

   assign
      tax_ref = vo_ref
      tax_nbr = pvo_internal_ref.

   /* ACCUMULATED TAXES */
   {apvotax.i}
 

end. /* if prh_type = "" */

/* RUN COSTING UPDATE ON INVENTORY SITE DATABASE */
if prh_type = ""
   or (prh_type = "S"
   and (pvo_cost_update

   or vph_adj_wip))
then do:
   assign
      l_po_site = (if prh_po_site <> "" then prh_po_site else pvo_shipto)
      old_db = global_db
      new_site = pvo_shipto.

   {gprun.i ""gpalias.p""}
/*GUI*/ if global-beam-me-up then undo, leave.

run calc-po-pur-cost(prh_pur_cost).   /* add by tony*/
   {gprun.i ""apvocsua.p""
               "(input old_db,
                 input ap_ref,
                 input ap_acct,
                 input ap_sub,
                 input ap_cc,
                 input vo_ref,
                 input vo_curr,
                 input vo_ex_rate,
                 input vo_ex_rate2,
                 input pod_wo_lot,
                 input pod_op,
                 input pvo_part,
                 input l_po_site,
                 input pvo_shipto,
                 input prh_type,
                 input pvo_cost_update,
                 input prh_um_conv,
                 input pvo_trans_qty,
                 input pvo_order,
                 input pvo_line,
                 input pvo_internal_ref,
                 input pod_project,
                 input /*prh_pur_cost*/ p-prh-pur-cost, /*modify by tony*/
                 input vph_adj_inv,
                 input vph_inv_cost,
                 input vph_inv_qty,
                 input vph_curr_amt,
                 input rndmthd,
                 input rate_var_amt,
                 input curr_cost_taxamt,
                 input taxamt,
                 input tax_on_receipt,
                 input tax_on_vph_price,
                 input-output totl_amt,
                 input-output unit_amt,
                 input-output vph_costadj_acct,
                 input-output vph_costadj_sub,
                 input-output vph_costadj_cc,
                 input-output vph_dscr_acct,
                 input-output vph_dscr_sub,
                 input-output vph_dscr_cc,
                 input-output vph_adj_amt,
                 input-output vph_adj_prv_cst,
                 input-output vph_qoh_at_adj,
                 input-output vph_dscr_amt,
                 input-output vph_cf_adj_amt,
                 input-output vph_cf_dscr_amt,
                 input-output vph_adj_wip
               )"}
/*GUI*/ if global-beam-me-up then undo, leave.


            /*DETERMINE COSTING METHOD*/
            {gprun.i ""csavg01.p"" "(input pvo_part,
                                     input pvo_shipto,
                                     output l_glx_set,
                                     output l_glx_mthd,
                                     output l_cur_set,
                                     output l_cur_mthd)"}
/*GUI*/ if global-beam-me-up then undo, leave.


   new_db = old_db.
   {gprun.i ""gpaliasd.p""}
/*GUI*/ if global-beam-me-up then undo, leave.

end.

/* STORING VALUES FROM vph_cf_adj_amt & vph_cf_dscr_amt INTO     */
/* l_vph_cf_adj_amt & l_vph_cf_dscr_amt RESPECTIVELY AND THEN    */
/* ROUNDING THESE VALUES SO THAT ROUNDED VALUES ARE PASSED AS    */
/* INPUT PARAMETERS FOR CALLING apvoglu1.p                       */

assign
   l_vph_cf_adj_amt  = vph_cf_adj_amt
   l_vph_cf_dscr_amt = vph_cf_dscr_amt.

{gprunp.i "mcpl" "p" "mc-curr-rnd"
    "(input-output l_vph_cf_adj_amt,
      input gl_rnd_mthd,
      output mc-error-number)"}

if mc-error-number <> 0
then do:
   {pxmsg.i &MSGNUM=mc-error-number &ERRORLEVEL=2}
end. /* IF mc-error-number <> 0 THEN */

{gprunp.i "mcpl" "p" "mc-curr-rnd"
    "(input-output l_vph_cf_dscr_amt,
      input gl_rnd_mthd,
      output mc-error-number)"}

if mc-error-number <> 0
then do:
   {pxmsg.i &MSGNUM=mc-error-number &ERRORLEVEL=2}
end. /* IF mc-error-number <> 0 THEN */

/* IF CONFIRMED COST ADJUSTMENT AMOUNT                      */
/* OR CONFIRMED COST ADJUSTMENT DISCREPANCY AMOUNT          */
/* ARE DIFFERENT FROM VOUCHER,                              */
/*   CREATE IC-TYPE GL TRANSACTIONS ON PO/AP DATABASE.      */

l_po_site = (if prh_po_site <> "" then prh_po_site else pvo_shipto).

/* CREATE "IC" GL FOR CONFIRMED COST ADJUSTMENT */

if (vph_adj_amt  <> 0
or  vph_dscr_amt <> 0)
and l_glx_mthd   =  "AVG"
then do:

   /* CHANGED SIXTEENTH INPUT PARAMETER FROM vph_cf_adj_amt TO     */
   /* l_vph_cf_adj_amt                                             */
   /* CHANGED SEVENTEENTH INPUT PARAMETER FROM vph_cf_dscr_amt TO  */
   /* l_vph_cf_dscr_amt                                            */
   {gprun.i ""apvoglu1.p""
               "(input ""AP"",
                 input ""ADD"",
                 input global_db,
                 input l_po_site,
                 input pvo_shipto,
                 input vph_costadj_acct,
                 input vph_costadj_sub,
                 input vph_costadj_cc,
                 input vph_dscr_acct,
                 input vph_dscr_sub,
                 input vph_dscr_cc,
                 input pod_project,
                 input pvo_order,
                 input vph_adj_amt,
                 input vph_dscr_amt,
                 input l_vph_cf_adj_amt,
                 input l_vph_cf_dscr_amt,
                 output l_gl_ref
               )"}
/*GUI*/ if global-beam-me-up then undo, leave.

end.

/*------------------------------------------------------------------*/

PROCEDURE calc-current-cost:

   define input parameter totl_amt as decimal.

      for each tx2d_det
            fields(tx2d_tax_in
            tx2d_cur_tax_amt
            tx2d_cur_recov_amt
            tx2d_totamt)
            where tx2d_ref     = vo_mstr.vo_ref
            and tx2d_nbr     = pvo_mstr.pvo_internal_ref
            and tx2d_line    = pvo_mstr.pvo_line
            and tx2d_tr_type = "22"
            and tx2d_tottax  <> 0
            no-lock:

         /* TAX INCLUDED = NO */
         if not tx2d_tax_in then

         /* CALCULATE PORTION OF NON-RECOVERABLE TAX     */
         /* WHICH NEEDS TO BE ADDED TO THE COST          */

         curr_cost_taxamt = curr_cost_taxamt
         + (totl_amt
         * (tx2d_cur_tax_amt - tx2d_cur_recov_amt)
         / tx2d_totamt).

         /* TAX INCLUDED = YES */
         else

         /* CALCULATE PORTION OF RECOVERABLE TAX          */
         /* WHICH NEEDS TO BE SUBTRACTED FROM COST        */

         curr_cost_taxamt = curr_cost_taxamt
         + (totl_amt
         * (- tx2d_cur_recov_amt)
         / tx2d_totamt).
 
      end. /* FOR EACH TAX DETAIL */
END PROCEDURE.  /* PROCEDURE calc-current-cost */

PROCEDURE calc-po-pur-cost:  /*add by tony*/

    define input parameter totl_amt as decimal.

      for each tx2d_det
            fields(tx2d_tax_in
            tx2d_cur_tax_amt
            tx2d_cur_recov_amt
            tx2d_totamt)
            where tx2d_ref     = vo_mstr.vo_ref
            and tx2d_nbr     = pvo_mstr.pvo_internal_ref
            and tx2d_line    = pvo_mstr.pvo_line
            and tx2d_tr_type = "22"
            and tx2d_tottax  <> 0
            no-lock:

         /* TAX INCLUDED = NO */
         if not tx2d_tax_in then

         /* CALCULATE PORTION OF NON-RECOVERABLE TAX     */
         /* WHICH NEEDS TO BE ADDED TO THE COST          */

         p-prh-pur-cost =totl_amt
         + (totl_amt
         * (tx2d_cur_tax_amt - tx2d_cur_recov_amt)
         / tx2d_totamt).

         /* TAX INCLUDED = YES */
         else

         /* CALCULATE PORTION OF RECOVERABLE TAX          */
         /* WHICH NEEDS TO BE SUBTRACTED FROM COST        */

         p-prh-pur-cost = totl_amt
         + (totl_amt
         * (- tx2d_cur_recov_amt)
         / tx2d_totamt).
 
      end. /* FOR EACH TAX DETAIL */

END PROCEDURE.  /* PROCEDURE calc-current-cost */